﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Script.Serialization;
using System.Collections;

namespace MvcControlsToolkit
{
    public abstract class Plot: Control
    {
        public string CssClass { get; set; }

        public bool StackSeries
        {
            get
            {
                if (Attributes.ContainsKey("stackSeries"))
                    return Boolean.Parse(GetString("stackSeries"));
                else
                    return true;
            }

            set
            {
                Attributes["stackSeries"] = value.ToString().ToLower();
            }
        }

        public string Title 
        {
        	get{
                return GetString("title");
        	}
            set{
                SetString("title", value);
            }
        }

        protected string Options
        {
            get
            {
                return Attributes["seriesDefaults"];
            }
            set
            {
                Attributes["seriesDefaults"] = value;
            }
        }

        protected string Legend
        {
            get
            {
                return Attributes["legend"];
            }
            set
            {
                Attributes["legend"] = value;
            }
        }

        protected string Grid{
            get
            {
                return Attributes["grid"];
            }
            set
            {
                Attributes["grid"] = value;
            }
        }

        protected string Axes
        {
            get
            {
                return Attributes["axes"];
            }
            set
            {
                Attributes["axes"] = value;
            }
        }

        protected abstract string GetData();

        protected Plot(string name) : base(name)
        {
        }
        
        public override string GetJScript()
        {
            var result = new StringBuilder();

            result.Append("[" + GetData() + "]");

            result.Append(", {");

            foreach (string key in Attributes.Keys)
            {
                result.AppendFormat("{0}: {1},", key, Attributes[key]);
            }

            if (result.Length > 0)
                result.Remove(result.Length-1, 1);

            result.Append(@"});");

            result.Insert(0, "var " + Name + " = $.jqplot(\"" + Name + "\", ");

            return result.ToString();
        }

        public override string GetHtml()
        {
            var html = new StringBuilder();

            html.Append(String.Format("<div id=\"{0}\" class=\"{1}\"></div>", Name, CssClass));

            return html.ToString();
        }        
    }
}
